package com.ftl.games;

import java.io.File;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Images.Media;
import android.text.format.Time;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

public class GriegoActivity extends Activity {
    private static final int CAMERA_PIC_REQUEST = 1337;
    private static final String DIR_ALBUM = "ftl_fotos";
    protected String imagenFinal;
    
    protected Uri uri;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        
        setContentView(R.layout.main);
        
        ImageView btnFoto = (ImageView) findViewById(R.id.uploadButton);
        
        btnFoto.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	//Creo el archivo fisico para la foto
            	File imageDirectory = new File("/sdcard/" + DIR_ALBUM);          
            	
            	String path = imageDirectory.toString().toLowerCase();            
            	String name = imageDirectory.getName().toLowerCase();               
            	ContentValues values = new ContentValues();              
            	//Cargo los datos de la nueva foto
            	values.put(Media.TITLE, "Image");              
            	values.put(Images.Media.BUCKET_ID, path.hashCode());             
            	values.put(Images.Media.BUCKET_DISPLAY_NAME,name);              
            	values.put(Images.Media.MIME_TYPE, "image/jpeg");             
            	values.put(Media.DESCRIPTION, "Fotito");            
            	//Genero un nombre unico para la foto
            	String nombreFoto =  String.valueOf(Time.MONTH_DAY) + String.valueOf(Time.MONTH) + String.valueOf(Time.HOUR) + String.valueOf(Time.MINUTE) + String.valueOf(Time.SECOND);
            	imagenFinal = "/sdcard/" + DIR_ALBUM + "/" + nombreFoto + ".jpg";
            	values.put("_data", imagenFinal);          
            	//Llamo a la camara
            	uri = getContentResolver().insert( Media.EXTERNAL_CONTENT_URI , values);             
            	Intent i = new Intent("android.media.action.IMAGE_CAPTURE");               
            	i.putExtra(MediaStore.EXTRA_OUTPUT, uri);              
            	startActivityForResult(i, CAMERA_PIC_REQUEST);  
            }
        });
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {   
        if (requestCode == CAMERA_PIC_REQUEST) {   
        	Bundle params = new Bundle();
            params.putString	("uri", uri.toString());
            params.putString	("imagenFinal"		, imagenFinal);
                        
            Intent i = new Intent(GriegoActivity.this,FBLogin.class);
            i.putExtras(params);
            startActivity(i);
        }
    	
        
  }
    
}